<template>
    <div class="payment-success-container">
      <div class="success-content">
        <!-- 成功图标 -->
        <el-icon class="success-icon" :size="80" color="#67C23A">
          <SuccessFilled />
        </el-icon>
  
        <!-- 标题 -->
        <h2 class="success-title">支付成功</h2>
  
        <!-- 订单信息 -->
        <div class="order-info">
          <div class="info-item">
            <span class="label">支付金额：</span>
            <span class="value">￥{{ paymentData.total_amount }}</span>
          </div>
          <div class="info-item">
            <span class="label">订单编号：</span>
            <span class="value">{{ paymentData.out_trade_no }}</span>
          </div>
          <div class="info-item">
            <span class="label">支付时间：</span>
            <span class="value">{{ paymentData.timestamp }}</span>
          </div>
        </div>
  
        <!-- 返回按钮 -->
        <el-button 
          type="primary" 
          class="back-btn" 
          @click="handleBack"
        >
          返回个人中心 ({{ countdown }}s)
        </el-button>
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref, onMounted, onBeforeUnmount } from 'vue'
  import { useRouter,useRoute } from 'vue-router'
  import { SuccessFilled } from '@element-plus/icons-vue'
import { queryOrderApi } from '@/api/order';
  
  // 路由器对象（跳转）
  const router = useRouter()


  // 支付数据（可以通过路由参数获取）
  interface PaymentData {
    total_amount: string
    out_trade_no: string
    timestamp: string
  }
  
  const paymentData = ref<PaymentData>({
    total_amount: '0.00',
    out_trade_no: '',
    timestamp: ''
  })
  
  // 倒计时
  const countdown = ref(5)
  let timer: number | null = null
  
  // 处理返回
  const handleBack = () => {
    router.replace('/personalcenter')
    if (timer) clearInterval(timer)
  }
  
  // 启动倒计时
  const startCountdown = () => {
    timer = setInterval(() => {
      if (countdown.value > 0) {
        countdown.value--
      } else {
        handleBack()
      }
    }, 1000)
  }

  /**
   * 根据订单编号查询订单信息，并更新支付数据
   */
  const queryOrder = async (out_trade_no:string) => {
    try {
      await queryOrderApi(out_trade_no) 
    }catch (error) {
      console.error('查询订单信息失败', error)
    }
  }
  
  onMounted(() => {
    // 实际开发中可以从路由参数获取支付信息
     const route = useRoute()
     paymentData.value = route.query
     queryOrder(paymentData.value.out_trade_no)
     startCountdown()
  })
  
  onBeforeUnmount(() => {
    if (timer) clearInterval(timer)
  })
  </script>
  
  <style lang="scss" scoped>
  .payment-success-container {
    height: 100vh;
    background-color: #f5f7fa;
    display: flex;
    justify-content: center;
    align-items: center;
  
    .success-content {
      text-align: center;
      background: white;
      padding: 40px 60px;
      border-radius: 8px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  
      .success-icon {
        animation: scaleIcon 0.5s ease-in-out;
      }
  
      .success-title {
        color: #67c23a;
        margin: 20px 0;
        font-size: 24px;
      }
  
      .order-info {
        margin: 30px 0;
        text-align: left;
  
        .info-item {
          line-height: 2;
          font-size: 16px;
  
          .label {
            color: #909399;
            margin-right: 10px;
          }
  
          .value {
            color: #606266;
            font-weight: 500;
          }
        }
      }
  
      .back-btn {
        width: 200px;
        margin-top: 20px;
      }
    }
  }
  
  @keyframes scaleIcon {
    0% {
      transform: scale(0);
      opacity: 0;
    }
    80% {
      transform: scale(1.2);
    }
    100% {
      transform: scale(1);
      opacity: 1;
    }
  }
  </style>